package com.zijie.system.controller.system;

import cn.dev33.satoken.annotation.SaIgnore;
import com.zijie.common.core.constant.Constants;
import com.zijie.common.web.core.BaseController;
import com.zijie.common.core.domain.R;
import com.zijie.common.core.domain.bo.RegisterBody;
import com.zijie.system.service.SysConfigService;
import com.zijie.system.service.SysRegisterService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 注册验证
 *
 * @author Lion Li
 */
@Validated
@RequiredArgsConstructor
@RestController
public class SysRegisterController extends BaseController {

    private final SysRegisterService registerService;
    private final SysConfigService configService;

    /**
     * 管理员注册
     */
    @SaIgnore
    @PostMapping("/register")
    public R<Void> register(@Validated @RequestBody RegisterBody user) {
        if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser")))) {
            return R.fail("当前系统没有开启注册功能！");
        }
        registerService.register(user);
        return R.ok();
    }
    @SaIgnore
    @GetMapping("/is_register")
    public R<Map<String, Object>> isRegister() {
        String flag = configService.selectConfigByKey("sys.account.registerUser");
        return R.ok(Map.of("flag", Boolean.parseBoolean(flag)));
    }

    /**
     * 用户注册
     */
    @SaIgnore
    @PostMapping("/user_register")
    public R<Void> userRegister(@Validated @RequestBody RegisterBody user) {
//        if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser")))) {
//            return R.fail("当前系统没有开启注册功能！");
//        }
        registerService.userRegister(user);
        return R.ok();
    }
}
